package com.bazaaroid.server.gae.core.dao.impl;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.bazaaroid.server.gae.core.dao.UserDAO;
import com.bazaaroid.server.gae.core.persistence.model.User;
import com.google.appengine.api.datastore.Key;

public class JDOUserDAOImpl extends JDOGenericDAOImpl<User, Key>
		implements UserDAO {

	public User getUserByUsername(String username) {
		PersistenceManager persistenceManager = getPersistenceManager();
		persistenceManager.setDetachAllOnCommit(true);

		Query query = persistenceManager.newQuery(getEntityClass());
		query.setFilter("username == usernameParam");
		query.declareParameters("String usernameParam");
		query.setUnique(true);

		User user = null;

		try {
			user = (User) query.execute(username);
		}
		finally {
			query.closeAll();
			persistenceManager.close();
		}

		return user;
	}
}
